package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.ParsingException;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.ARRAY;
import ar.edu.caece.compiler.grammar.nonterminals.OTROSA;
import ar.edu.caece.compiler.grammar.nonterminals.PASOPAM1;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;
import ar.edu.caece.compiler.grammar.terminals.IdentifierToken;

public class PASOPAM1Factory extends AbstractTokenVisitorFactory {

	private static PASOPAM1Factory INSTANCE;
	
	private OTROSAFactory otrosaFactory;
	private TokenVisitorInterface parseOTROSA;
	
	private ARRAYFactory arrayFactory;
	private TokenVisitorInterface parseARRAY;
	
	public static PASOPAM1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new PASOPAM1Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			otrosaFactory = OTROSAFactory.getInstance();
			parseOTROSA = otrosaFactory.makeVisitor();
			
			arrayFactory = ARRAYFactory.getInstance();
			parseARRAY = arrayFactory.makeVisitor();
		}
	};
	
	private PASOPAM1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierChainVisitor(siguiente) {
			
			@Override
			public GrammarSymbol identifierCase(IdentifierToken llamador) throws Exception {
				return new PASOPAM1(llamador, (ARRAY) nextToken().execute(parseARRAY), (OTROSA) nextToken().execute(parseOTROSA));
			}
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierTokenVisitor() {
			
			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				throw new ParsingException("Error de parsing.",PASOPAM1.class,llamador);
			}
			
			@Override
			public GrammarSymbol identifierCase(IdentifierToken llamador) throws Exception {
				return new PASOPAM1(llamador, (ARRAY) nextToken().execute(parseARRAY),(OTROSA) nextToken().execute(parseOTROSA));
			}
		};
	}
	
	
}
